<?php

/**
 * @author Thanh Nguyen Dinh <thanhnguyendinh@admicro.vn>
 * @copyright 2012
 */
class Breadcrumb
{

    private $_bread = '';
    private $_crumbs = array();
    private $_href_param;
    private $_seperator;
    private $_home_text;
    private $_home_link;

    function Breadcrumb($config = array())
    {
        $this->_seperator = array_key_exists('seperator', $config) ? $config['seperator'] : "&nbsp;>&nbsp;";
        $this->_href_param = array_key_exists('href_param', $config) ? $config['href_param'] : NULL;
        $this->_home_link = array_key_exists('home_link', $config) ? $config['home_link'] : '/';
        $this->_home_text = array_key_exists('home_text', $config) ? $config['home_text'] : "Home";
        $this->_crumbs[] = array('crumb' => $this->_home_text, 'link' => $this->_home_link);
    }

    function addCrumb($this_crumb, $this_link = NULL)
    {
        $in_crumbs = false;

        foreach ($this->_crumbs as $crumb) {
            if ($crumb['crumb'] == $this_crumb) {
                $in_crumbs = true;
            }
            if ($crumb['link'] == $this_link && $this_link != '') {
                $in_crumbs = true;
            }
        }
        if ($in_crumbs == false) {
            $this->_crumbs[] = array('crumb' => $this_crumb, 'link' => $this_link);
        }
    }

    function makeBread()
    {
        $sandwich = $this->_crumbs;
        $slices = array();
        $countBread = count($sandwich);
        $i = 1;
        foreach ($sandwich as $slice) {
            if (isset($slice['link']) && $slice['link'] != '') {
                if ($i != $countBread)
//                    $slices[] = '<a href="' . $slice['link'] . '" ' . $this->_href_param . '>' . $slice['crumb'] . '</a>';
                    $slices[] = "<li itemscope='' itemtype='http://data-vocabulary.org/Breadcrumb'>
                                    <a href='{$slice['link']}' itemprop='url' {$this->_href_param}>
                                        <span itemprop='title'>".$slice['crumb']."</span>
                                    </a>
                                </li>";
                else
                    $slices[] = "<li itemscope='' itemtype='http://data-vocabulary.org/Breadcrumb'>
                                    <span itemprop='title'>".$slice['crumb']."</span>
                                </li>";
            } else {
                $slices[] = "<li itemscope='' itemtype='http://data-vocabulary.org/Breadcrumb'>
                                <span itemprop='title'>".$slice['crumb']."</span>
                            </li>";
            }
            $i++;
        }
        $this->_bread = "<ul class='breadcrumb'>" . join($this->_seperator, $slices) . "</ul>";
        return $this->_bread;
    }

}

?>
